var express = require('express');
var router = express.Router();
var settings = require('../config');
var request = require('request');
var async = require('async');

/* GET home page. */
module.exports = function(app) {
    var heads=[];
    app.get('/', function (req, res) {
        //获取服务类型
        request.get({url:settings.url+'/lifeService/getServiceTypeList'},function(error, httpResponse, body){
            if(!error && httpResponse.statusCode == 200) {
                var menu=JSON.parse(body);
                var hh='';
                var header=[];
                var content=[];
                var listData=[];
                var sid=menu[0].serv_type;
                async.waterfall([
                    function(callback){
                        for ( i = 0; i < menu.length; i++) {//循环获取服务类型
                            if(hh==menu[i].serv_type){
                                continue;
                            }else{
                                hh=menu[i].serv_type;
                                header.push(menu[i].serv_type);
                            }
                        }
                        callback(null, header);
                    },
                    function(header,callback){
                        for ( i = 0; i < header.length; i++) {
                            for ( j = 0; j < menu.length; j++) {
                                //alert(header[i]+' '+pdata[j].serv_name);
                                if(header[i]==menu[j].serv_type){//如果类型等于当前类型放入同一个数组
                                    content.push({
                                        name:menu[j].serv_name,
                                        servid:menu[j].serv_id
                                    });
                                }
                                if(j==(menu.length-1)){//循环完一次
                                    listData.push({header:header[i],content:content});
                                    content=[];//清空
                                }
                            }
                        }
                        callback(null, listData);
                    }
                ], function (err, listData) {
                    request.post({url:settings.url+'/lifeService/lifeServiceList',form:{page:"1",rows:'10',sort:"createtime",order:"desc",serv:sid,serv_id:""}},function optionalCallback(error, httpResponse, body){
                        if (!error && httpResponse.statusCode == 200) {
                            console.log(body);
                            var count = 0;
                            var order=JSON.parse(body);
                            var datas=[];
                            async.whilst(function(){
                                return count<order.rows.length;
                            },function(callback) {
                                request.post({url: settings.url + '/lifeService/lifeServiceGoodsList', form: {page: "1", rows: '10', sort: "createtime", order: "desc", servid: order.rows[count].lid}}, function (err, httpResponse, body) {
                                    var orders = JSON.parse(body);
                                    if(orders.rows.length==0){
                                        count++;
                                        callback();
                                    }else{
                                        for(var i=0;i<orders.rows.length;i++){
                                            datas.push(orders.rows[i]);
                                        }
                                        count++;
                                        callback();
                                    }
                                });
                            },function(err){
                                //根据商户ID 获取商品ID
                                var height = Math.ceil(datas.length / 2) * 400;
                                //获取城市
                                request.get({url:settings.url+'/getCityList'},function(error,httpResponse, body){
                                    var citys=JSON.parse(body);
                                    heads.push(listData);
                                    res.render('index', { title: 'Carsremit', order: datas, height: height,menu:listData,citys:citys,sid:sid});
                                });
                            });
                        }else{
                            console.log("lifeServiceGoodsList error");
                        }
                    });
                });
            }
        });
    });
    app.post('/serviceType',function(req,res){
        var sid=req.body.sid;
        var listh = heads[0];
        async.waterfall([
            function(callback){
                console.log(sid);
                for(var i=0;i<listh.length;i++){
                    if(sid==listh[i].header){
                        var types=listh[i].content;
                        var serid=listh[i];
                        callback(null, types);
                    }
                }
            }
        ], function (err, result) {
            var types=result;
            console.log(types);
            //根据类型ID,查询ID下的商户，把商户ID放入集合中
            request.post({url:settings.url+'/lifeService/lifeServiceList',form:{page:"1",rows:'10',sort:"createtime",order:"desc",serv:sid,serv_id:""}},function optionalCallback(error, httpResponse, body){
                if (!error && httpResponse.statusCode == 200) {
                    var count = 0;
                    var order=JSON.parse(body);
                    var lids=[];
                    var datas=[];
                    async.whilst(function(){
                        return count<order.rows.length;
                    },function(callback) {
                        request.post({url: settings.url + '/lifeService/lifeServiceGoodsList', form: {page: "1", rows: '10', sort: "createtime", order: "desc", servid: order.rows[count].lid}}, function (err, httpResponse, body) {

                            var orders = JSON.parse(body);
                            if(orders.rows.length==0){
                                count++;
                                callback();
                            }else{
                                for(var i=0;i<orders.rows.length;i++){
                                    datas.push(orders.rows[i]);
                                }
                                count++;
                                callback();
                            }
                        });
                    },function(err){
                        //根据商户ID 获取商品ID
                        var height = Math.ceil(datas.length / 2) * 400;
                        res.send({ title: 'Carsremit', order: datas, height: height,types:types,sid:sid});
                    });
                }else{
                    console.log("lifeService error");
                }
            });
        });
    });
    app.post('/chooseCity',function(req,res){
        var serv=req.body.serv;
        var servid=req.body.servid;
        var cityid=req.body.cityid;
        if(servid==0){
            servid="";
        }
        if(cityid==0){
            cityid="";
        }
        request.post({url:settings.url+'/lifeService/lifeServiceList',form:{page:"1",rows:'10',sort:"createtime",order:"desc",cityid:cityid,serv:serv,serv_id:servid}},function(error, httpResponse, body){
            if (!error && httpResponse.statusCode == 200) {
                var count = 0;
                var order=JSON.parse(body);
                var lids=[];
                var datas=[];
                async.whilst(function(){
                    return count<order.rows.length;
                },function(callback) {
                    request.post({url: settings.url + '/lifeService/lifeServiceGoodsList', form: {page: "1", rows: '10', sort: "createtime", order: "desc", servid: order.rows[count].lid}}, function (error, httpResponse, body) {

                        var orders = JSON.parse(body);
                        if(orders.rows.length==0){
                            count++;
                            callback();
                        }else{
                            for(var i=0;i<orders.rows.length;i++){
                                datas.push(orders.rows[i]);
                            }
                            count++;
                            callback();
                        }
                    });
                },function(err){
                    //根据商户ID 获取商品ID
                    var height = Math.ceil(datas.length / 2) * 400;
                    res.send({ title: 'Carsremit', order: datas, height: height});
                });
            }else{
                console.log("lifeServiceGoodsList error");
            }
        });
    });
    app.post('/chooseType',function(req,res){
        var serv=req.body.serv;
        var servid=req.body.servid;
        if(servid==0){
            servid="";
        }
        request.post({url:settings.url+'/lifeService/lifeServiceList',form:{page:"1",rows:'10',sort:"createtime",order:"desc",serv:serv,serv_id:servid}},function(error, httpResponse, body){
            if (!error && httpResponse.statusCode == 200) {
                var count = 0;
                var order=JSON.parse(body);
                var lids=[];
                var datas=[];
                async.whilst(function(){
                    return count<order.rows.length;
                },function(callback) {
                    request.post({url: settings.url + '/lifeService/lifeServiceGoodsList', form: {page: "1", rows: '10', sort: "createtime", order: "desc", servid: order.rows[count].lid}}, function (error, httpResponse, body) {

                        var orders = JSON.parse(body);
                        if(orders.rows.length==0){
                            count++;
                            callback();
                        }else{
                            for(var i=0;i<orders.rows.length;i++){
                                datas.push(orders.rows[i]);
                            }
                            count++;
                            callback();
                        }
                    });
                },function(err){
                    //根据商户ID 获取商品ID
                    var height = Math.ceil(datas.length / 2) * 400;
                    res.send({ title: 'Carsremit', order: datas, height: height});
                });
            }else{
                console.log("lifeServiceGoodsList error");
            }
        });
    });
}
